//
//  UserModel.m
//  DoctorQA
//
//  Created by sally on 16/6/20.
//  Copyright © 2016年 talecare. All rights reserved.
//

#import "UserModel.h"

@implementation UserModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

+ (JSONKeyMapper *)keyMapper {
  return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id" : @"uid" }];
}

//发送验证码
+ (void)sendCaptchaWithPhone:(NSString *)phone
                  completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t_code _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description, BOOL isSuccess) {

    if (code == SUCCESS_RESULT && isSuccess) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface sendCaptchaWithPhone:phone completion:_completion error:_error];
}

//验证验证码并注册
+ (void)registerWithPhone:(NSString *)phone
                  captcha:(NSString *)captcha
                 password:(NSString *)password
               completion:(void (^)(UserModel *user))completion {
  [self cleanLastError];

  completion_t_user _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description, BOOL isNewUser) {
    UserModel *user = nil;

    if (code == SUCCESS_RESULT) {
      user = [[UserModel alloc] initWithDictionary:resultObject error:nil];
      user.user_data.new_user = isNewUser;
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
    }

    completion(user);
  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface registerWithPhone:phone captcha:captcha password:password completion:_completion error:_error];
}

//完善用户资料
+ (void)updateUserInfoWithUserName:(NSString *)userName
                    userProfilePic:(NSString *)userProfilePic
                      locationName:(NSString *)locationName
                            bigPic:(NSString *)bigPic
                           proDesc:(NSString *)proDesc
                          proTitle:(NSString *)proTitle
                        topicIndex:(NSString *)topicIndex
                       topicIndex2:(NSString *)topicIndex2
                        completion:(void (^)(UserModel *user))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    UserModel *user = nil;
    NSMutableArray *resultArr = (NSMutableArray *) resultObject;
    if (code == SUCCESS_RESULT) {
      user = [[UserModel alloc] initWithDictionary:resultArr[0] error:nil];
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
    }

    completion(user);
  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface updateUserInfoWithUserName:userName userProfilePic:userProfilePic locationName:locationName bigPic:bigPic proDesc:proDesc proTitle:proTitle topicIndex:topicIndex topicIndex2:topicIndex2 completion:_completion error:_error];
}

//登录
+ (void)loginWithPhone:(NSString *)phone
              password:(NSString *)password
            completion:(void (^)(UserModel *user))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    UserModel *user = nil;

    if (code == SUCCESS_RESULT) {
      user = [[UserModel alloc] initWithDictionary:resultObject error:nil];
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
    }

    completion(user);
  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface loginWithPhone:phone password:password completion:_completion error:_error];
}

//设置用户密码
+ (void)modifyPswWithPhone:(NSString *)phone
               oldPassword:(NSString *)oldPassword
                  password:(NSString *)password
                confirmPwd:(NSString *)confirmPwd
                completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface modifyPswWithPhone:phone oldPassword:oldPassword password:password confirmPwd:confirmPwd completion:_completion error:_error];
}

//重置用户密码
+ (void)resetPswWithPhone:(NSString *)phone
                 password:(NSString *)password
                  captcha:(NSString *)captcha
               completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface resetPswWithPhone:phone password:password captcha:captcha completion:_completion error:_error];
}

//收藏专家到心愿单
+ (void)collectExpertWithProUid:(NSInteger)proUid
                     completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface collectExpertWithProUid:proUid completion:_completion error:_error];
}

//取消收藏专家
+ (void)cancelCollectExpertWithProUid:(NSInteger)proUid
                           completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface cancelCollectExpertWithProUid:proUid completion:_completion error:_error];
}

//用户反馈
+ (void)submitFeedbackWithPhone:(NSString *)phone
                        content:(NSString *)content
                     completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface submitFeedbackWithPhone:phone content:content completion:_completion error:_error];
}

//当前用户信息接口
+ (void)quaryUserInfoWithCompletion:(void (^)(UserModel *user))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    UserModel *user = nil;

    if (code == SUCCESS_RESULT) {
      NSDictionary *listResultDic = resultObject[DicKeyList];
      NSMutableArray *resultArr = (NSMutableArray *) listResultDic;
      if (resultArr.count > 0) {
        user = [[UserModel alloc] initWithDictionary:resultArr[0] error:nil];
      }
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
    }

    completion(user);
  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface quaryUserInfoWithCompletion:_completion error:_error];
}

//提现绑定支付宝
+ (void)bindAlipayWithAlipay:(NSString *)alipay
                  completion:(void (^)(UserModel *user))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    UserModel *user = nil;

    if (code == SUCCESS_RESULT) {
      NSMutableArray *resultList = (NSMutableArray *) resultObject;
      if (resultList && ![resultList isKindOfClass:[NSNull class]] && resultList.count > 0) {
        user = [[UserModel alloc] initWithDictionary:resultList[0] error:nil];
      }
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
    }

    completion(user);
  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface bindAlipayWithAlipay:alipay completion:_completion error:_error];
}

@end

@implementation UserData

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

@end

@implementation NewsDetailModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

@end

@implementation MessageModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

- (instancetype)initWithNote:(NSString *)text sender:(MessageFrom_E)sender type:(MessageType_E)type {
  self = [super init];
  if (self) {
    self.text = text;
    self.sender = sender;
    self.type = type;
    self.model.avatar = @"http://shq-pic.b0.upaiyun.com/Attachment/face/002/38/33/58_avatar.jpg";
    self.model.dateline = @"1455761739";
    self.model.pic = @"http://shq-pic.b0.upaiyun.com/Attachment/face/002/38/33/58_avatar.jpg";
    self.model.note = @"图片卡note";
    self.model.message_str = @"图片卡内容";
  }
  return self;
}

@end

@implementation Ext

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

@end

@implementation PicModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

//上传图片
+ (void)uploadPicWithImage:(UIImage *)image
                completion:(void (^)(PicModel *pic))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    PicModel *pic = nil;

    if (code == SUCCESS_RESULT) {
      pic = [[PicModel alloc] initWithDictionary:resultObject error:nil];
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
    }

    completion(pic);
  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface uploadPicWithImage:image completion:_completion error:_error];
}

@end
